using InvestmentIntelligence.DB;
using InvestmentIntelligence.DbModel.Models;
using InvestmentIntelligence.DbModel.Models.Compass;
using InvestmentIntelligence.DbModel.Models.Mapping;
using InvestmentIntelligence.DbModel.Models.Other;
using System;
using System.Data.Entity;

namespace InvestmentIntelligence.DbModel
{
    [DbConfigurationType(typeof(Configuration))]
    public class IIPEntities : DbContext
    {
        static IIPEntities()
        {
            Database.SetInitializer<IIPEntities>(null);
        }

        public IIPEntities()
            : base(GetConnectionString())
        {
            Configuration.LazyLoadingEnabled = false;
        }


        public IIPEntities(string connectionString)
            : base(connectionString)
        {
            Configuration.LazyLoadingEnabled = false;
        }

        public DbSet<AnalysisGroup> AnalysisGroups { get; set; }
        public DbSet<BenchmarkList> BenchmarkLists { get; set; }
        public DbSet<BookBenchmarkType> BookBenchmarkTypes { get; set; }
        public DbSet<BookSecurityToBenchmark> BookSecurityToBenchmarks { get; set; }
        //public DbSet<CompliancePeriod> CompliancePeriods { get; set; }
        public DbSet<Contact> Contacts { get; set; }
        public DbSet<CorporateEvent> CorporateEvents { get; set; }
        public DbSet<Country> Countries { get; set; }
        public DbSet<Currency> Currencies { get; set; }
        public DbSet<Widget> DesktopWindows { get; set; }
        public DbSet<EconomicSector> EconomicSectors { get; set; }
        public DbSet<Entity> Entities { get; set; }
        public DbSet<ExcessTimeSerie> ExcessTimeSeries { get; set; }
        public DbSet<FileUpload> FileUploads { get; set; }
        public DbSet<FundBookEntity> FundBookEntities { get; set; }
        public DbSet<FundBookEntitySerie> FundBookEntitySeries { get; set; }
        public DbSet<FundBookEntityActiveSerie> FundBookEntityActiveSeries { get; set; }
        public DbSet<InvestmentRiskSerie> InvestmentRiskSeries { get; set; }
        public DbSet<InvestmentSecuritySerie> InvestmentSecuritySeries { get; set; }
        public DbSet<InvestmentSecurityUnderlyingSerie> InvestmentSecurityUnderlyingSeries { get; set; }
        public DbSet<InvestmentBenchmarkSerie> InvestmentBenchmarkSeries { get; set; }
        public DbSet<InvestmentBenchmarkUnderlyingSerie> InvestmentBenchmarkUnderlyingSeries { get; set; }
        //public DbSet<AnalyticsIdeaToSecurity> IdeaToSecurities { get; set; }

        public DbSet<Investment> Investments { get; set; }
        public DbSet<Layout> Layouts { get; set; }
        public DbSet<Manager> Managers { get; set; }
        public DbSet<OriginalPosition> OriginalPositions { get; set; }
        public DbSet<OriginalPositionValidationData> OriginalPositionValidationData { get; set; }
        public DbSet<BookSecurityState> BookSecurityStates { get; set; }
        public DbSet<Parameter> Parameters { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<Security> Securities { get; set; }
        public DbSet<SecurityClass> SecurityClasses { get; set; }
        public DbSet<SecurityView> SecurityViews { get; set; }
        public DbSet<SuspiciousExposure> SuspiciousExposures { get; set; }

        // Analytics Idea
        public DbSet<AnalyticsIdea> AnalyticsIdeas { get; set; }
      

        public DbSet<TimeSerie> TimeSeries { get; set; }
        public DbSet<TracePoint> TracePoints { get; set; }
        public DbSet<TradeConsistencyCheck> TradeConsistencyChecks { get; set; }
        public DbSet<TradeUpdate> TradeUpdates { get; set; }
        public DbSet<TradedQuantity> TradedQuantities { get; set; }
        public DbSet<Trade> Trades { get; set; }
        public DbSet<LogEntry> LogEntries { get; set; }
        public DbSet<UsedSecurity> UsedSecurities { get; set; }
        public DbSet<User2Role> User2Role { get; set; }
        
        public DbSet<User> Users { get; set; }
        //public DbSet<Tag> Tags { get; set; }
        public DbSet<UnderlyingReplacement> UnderlyingReplacements { get; set; }

        //Logging
        public DbSet<ActionLog> ActionLog { get; set; }

        public DbSet<TradeDetailedView> TradeSummaries { get; set; }
        public DbSet<TaskStats> TaskStats { get; set; }

        public virtual int UpdateParentId()
        {
            return Database.ExecuteSqlCommand("exec TradingData.UpdateParentId");
        }

        public virtual int DeletePositionWithValidationDataParentId(int securityId, int fundbookId, DateTime date)
        {
            const string comand = @" delete from [TradingData].OriginalPositionValidationData where SecurityId={0} AND FundBookEntityId ={1} AND [Date] ={2}
                                     delete from [TradingData].OriginalPosition where SecurityId={0} AND FundBookEntityId ={1} AND [Date] ={2}";
            return Database.ExecuteSqlCommand(comand, securityId, fundbookId, date.Date);
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new InvestmentMap());
            modelBuilder.Configurations.Add(new InvestmentSecuritySerieMap());
            modelBuilder.Configurations.Add(new InvestmentBenchmarkSerieMap());
            modelBuilder.Configurations.Add(new InvestmentBenchmarkUnderlyingSerieMap());
            modelBuilder.Configurations.Add(new SuspiciousExposureMap());
            modelBuilder.Configurations.Add(new AnalysisGroupMap());

            // Analytics Idea
            modelBuilder.Configurations.Add(new AnalyticsIdeaMap());
           

            modelBuilder.Configurations.Add(new ContactMap());
            modelBuilder.Configurations.Add(new EntityMap());
            modelBuilder.Configurations.Add(new RoleMap());
            modelBuilder.Configurations.Add(new UserMap());
            modelBuilder.Configurations.Add(new User2RoleMap());
          
            
            modelBuilder.Configurations.Add(new FileUploadMap());
            modelBuilder.Configurations.Add(new BenchmarkListMap());
            modelBuilder.Configurations.Add(new BookBenchmarkTypeMap());
            modelBuilder.Configurations.Add(new CorporateEventMap());
            modelBuilder.Configurations.Add(new CountryMap());
            modelBuilder.Configurations.Add(new CurrencyMap());
            modelBuilder.Configurations.Add(new EconomicSectorMap());
            modelBuilder.Configurations.Add(new FundBookEntityMap());
            modelBuilder.Configurations.Add(new ManagerMap());
            modelBuilder.Configurations.Add(new OriginalPositionMap());
            modelBuilder.Configurations.Add(new BookSecurityStateMap());
            modelBuilder.Configurations.Add(new SecurityClassMap());
            modelBuilder.Configurations.Add(new TradeMap());
            modelBuilder.Configurations.Add(new TradeConsistencyCheckMap());
            modelBuilder.Configurations.Add(new UsedSecurityMap());
            modelBuilder.Configurations.Add(new TradedQuantityMap());
            modelBuilder.Configurations.Add(new TracePointMap());
            modelBuilder.Configurations.Add(new LogEntryMap());
            modelBuilder.Configurations.Add(new InvestmentRiskSerieMap());
            modelBuilder.Configurations.Add(new InvestmentSecurityUnderlyingSerieMap());
            modelBuilder.Configurations.Add(new ActionLogMap());
            modelBuilder.Configurations.Add(new TaskStatsMap());
        }

        private static string GetConnectionString()
        {
            return IIPConnectionString.GetConnectionString();
        }
    }
}
